package com.huaweicloud.metastudio.videodrive;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.metastudio.v1.MetaStudioClient;
import com.huaweicloud.sdk.metastudio.v1.model.CreateVideoMotionCaptureJobResponse;
import com.huaweicloud.sdk.metastudio.v1.model.ListVideoMotionCaptureJobsRequest;
import com.huaweicloud.sdk.metastudio.v1.model.ListVideoMotionCaptureJobsResponse;
import com.huaweicloud.sdk.metastudio.v1.model.ExecuteVideoMotionCaptureCommandResponse;
import com.huaweicloud.sdk.metastudio.v1.model.ShowVideoMotionCaptureJobResponse;
import com.huaweicloud.sdk.metastudio.v1.model.ExecuteVideoMotionCaptureCommandRequest;
import com.huaweicloud.sdk.metastudio.v1.model.StopVideoMotionCaptureJobRequest;
import com.huaweicloud.sdk.metastudio.v1.model.StopVideoMotionCaptureJobResponse;
import com.huaweicloud.sdk.metastudio.v1.model.OutputInfo;
import com.huaweicloud.sdk.metastudio.v1.model.VideoMotionCaptureJobReq;
import com.huaweicloud.sdk.metastudio.v1.model.CreateVideoMotionCaptureJobRequest;
import com.huaweicloud.sdk.metastudio.v1.model.ShowVideoMotionCaptureJobRequest;
import com.huaweicloud.sdk.metastudio.v1.model.ControlDigitalHumanLiveReq;
import com.huaweicloud.sdk.metastudio.v1.region.MetaStudioRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;

public class MetaStudioVideoDrive {
    private static final Logger logger = LoggerFactory.getLogger(MetaStudioVideoDrive.class);

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Start HUAWEI CLOUD MetaStudio Video Drive Java Demo...");
        ICredential auth = getCredential("YOUR AK", "YOUR SK","YOUR PROJECT ID");

        // 初始数字人服务的客户端
        MetaStudioClient client = getClient(MetaStudioRegion.CN_NORTH_4, auth);


        // 创建视频驱动任务
        CreateVideoMotionCaptureJobResponse videoMotionCaptureJob = createVideoMotionCaptureJob(client);
        Thread.sleep(5000);

        // 获取视频驱动任务列表
        listVideoMotionJob(client);

        // 查询视频驱动详情
        showVideoMotionCaptureJob(client, videoMotionCaptureJob.getJobId());

        // 控制视频驱动
        executeVideoMotionCaptureCommandJob(client, videoMotionCaptureJob.getJobId());
        Thread.sleep(5000);

        // 停止视频驱动
        stopVideoMotionCaptureJob(client, videoMotionCaptureJob.getJobId());
    }


    /**
     * 查询视频驱动任务列表
     *
     * @param client MetaStudio client
     * @return 查询视频驱动任务列表响应
     */
    public static ListVideoMotionCaptureJobsResponse listVideoMotionJob(MetaStudioClient client) {
        System.out.println("listVideoMotionJob start");
        try {
            ListVideoMotionCaptureJobsRequest listVideoMotionCaptureJobsRequest = new ListVideoMotionCaptureJobsRequest();
            ListVideoMotionCaptureJobsResponse response = client.listVideoMotionCaptureJobs(listVideoMotionCaptureJobsRequest);
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }


    /**
     * 创建视频驱动任务
     *
     * @param client MetaStudio client
     * @return 创建视频驱动任务响应
     */
    public static CreateVideoMotionCaptureJobResponse createVideoMotionCaptureJob(MetaStudioClient client) {
        // 设置接收视频驱动数据的地址
        OutputInfo outputInfo = new OutputInfo();
        UUID uuid = UUID.randomUUID();
        int randomInt = uuid.hashCode();
        randomInt = randomInt < 0 ? -randomInt : randomInt;
        outputInfo.setAudioAddr("121.36.196.49:30002"); // 音频输入地址
        outputInfo.setBodyAddr("121.36.196.49:30002"); // 面部表情输入地址
        outputInfo.setFaceAddr("121.36.196.49:30002"); // 面部表情输入地址
        // 会话ID
        outputInfo.setSessionId(randomInt);

        VideoMotionCaptureJobReq videoMotionCaptureJobReq = new VideoMotionCaptureJobReq();
        // 设置视频驱动模式
        videoMotionCaptureJobReq.setMotionCaptureMode(VideoMotionCaptureJobReq.MotionCaptureModeEnum.FULL_BODY);
        videoMotionCaptureJobReq.setOutputInfo(outputInfo);
        try {
            CreateVideoMotionCaptureJobRequest createVideoMotionCaptureJobRequest = new CreateVideoMotionCaptureJobRequest();
            createVideoMotionCaptureJobRequest.withBody(videoMotionCaptureJobReq);
            CreateVideoMotionCaptureJobResponse response = client.createVideoMotionCaptureJob(createVideoMotionCaptureJobRequest);
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 查询视频驱动任务详情
     *
     * @param client MetaStudio client
     * @param jobId 视频驱动任务ID
     * @return 查询视频驱动任务详情响应
     */
    public static ShowVideoMotionCaptureJobResponse showVideoMotionCaptureJob(MetaStudioClient client, String jobId) {
        ShowVideoMotionCaptureJobResponse response = null;
        try {
            System.out.println("showVideoMotionCaptureJob start");
            ShowVideoMotionCaptureJobRequest showVideoMotionCaptureJobRequest = new ShowVideoMotionCaptureJobRequest();
            showVideoMotionCaptureJobRequest.withJobId(jobId);
            response = client.showVideoMotionCaptureJob(showVideoMotionCaptureJobRequest);
            System.out.println("ShowVideoMotionCaptureJobResponse:" + response.toString());
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 控制视频驱动，包括视觉驱动复位、模型位移调整等
     *
     * @param client MetaStudio client
     * @param jobId 视频驱动任务ID
     * @return 控制视频驱动响应
     */
    public static ExecuteVideoMotionCaptureCommandResponse executeVideoMotionCaptureCommandJob(MetaStudioClient client, String jobId) {
        System.out.println("executeVideoMotionCaptureCommandJob start");
        ExecuteVideoMotionCaptureCommandRequest executeVideoMotionCaptureCommandRequest = new ExecuteVideoMotionCaptureCommandRequest().withJobId(jobId);
        ControlDigitalHumanLiveReq controlDigitalHumanLiveReq = new ControlDigitalHumanLiveReq().withCommand(ControlDigitalHumanLiveReq.CommandEnum.BODY_POS_RESET);
        executeVideoMotionCaptureCommandRequest.setBody(controlDigitalHumanLiveReq);
        try {
            ExecuteVideoMotionCaptureCommandResponse response = client.executeVideoMotionCaptureCommand(executeVideoMotionCaptureCommandRequest);
            System.out.println("ExecuteVideoMotionCaptureCommandResponse" + response);
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 停止视频驱动任务
     *
     * @param client MetaStudio client
     * @param jobId 视频驱动任务ID
     * @return 停止视频驱动任务响应
     */
    public static StopVideoMotionCaptureJobResponse stopVideoMotionCaptureJob(MetaStudioClient client, String jobId) {
        System.out.println("stopVideoMotionCaptureJob start");
        StopVideoMotionCaptureJobRequest stopVideoMotionCaptureJobRequest = new StopVideoMotionCaptureJobRequest().withJobId(jobId);
        try {
            StopVideoMotionCaptureJobResponse response = client.stopVideoMotionCaptureJob(stopVideoMotionCaptureJobRequest);
            return response;
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println("there is some error, exception:" + e);
        }
        return null;
    }

    /**
     * 创建鉴权凭证
     *
     * @param ak 租户帐号对应的 Access Key（AK）
     * @param sk 租户帐号对应的 Secret Access Key（SK）
     * @return 鉴权凭证
     */
    public static ICredential getCredential(String ak, String sk, String projectId) {
        return new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
    }

    /**
     * 创建MetaStudio client
     *
     * @param region region信息
     * @param auth 鉴权凭证
     * @return MetaStudio client
     */
    public static MetaStudioClient getClient(Region region, ICredential auth) {
        // 使用默认配置
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // 初始化metaStudio服务的客户端
        return MetaStudioClient.newBuilder()
                .withHttpConfig(config)
                .withCredential(auth)
                .withRegion(region) // 选择服务所在区域
                .build();
    }
}
